package co2.bean;

import java.io.Serializable;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;

import org.hibernate.id.UUIDHexGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import co2.engine.DAOInterface;
import co2.engine.SessionValidation;
import co2.pojos.AnggotaKelompok;
import co2.pojos.Kelompok;
import co2.pojos.MstUser;
import co2.pojos.SoalLks;

@ManagedBean
@ViewScoped
public class LksBean extends AbstractBean implements Serializable {
	private static final long serialVersionUID = 1L;
	
	@Autowired
	@Qualifier("daoI")
	private DAOInterface dao;
	
	private MstUser user;
	private Kelompok kelompok;
	private List<MstUser> listAnggota;
	
	private SoalLks soal;
	private boolean spawn=false;
	private String css;
	
	
	
	
	@PostConstruct
	public void postCon(){
		SignInBean signInBean = SessionValidation.checkSession();
		
		if(signInBean!=null){
			user = signInBean.getTheUser();
			kelompok = dao.getKelompokForUser(user.getUserId());
			if(kelompok!=null&&kelompok.getId()!=null){
				listAnggota = dao.getDaftarAnggotaKelompok(kelompok.getId());
				soal = dao.getSoalLksByKelompokId(kelompok.getId());
			}
		}
		
		if(soal == null){
			soal = new SoalLks();
		}
		
		css = "jwb-unview";
		spawn = false;
	}
	
	public void toKelompok(){
		try{
			String path = FacesContext.getCurrentInstance().getExternalContext().getRequestContextPath();
			FacesContext.getCurrentInstance().getExternalContext().redirect(path+"/jsf/target/kelompok.jsf");
		}catch(Exception e){
			e.printStackTrace();
		}
	} 
	public void submit(){
		try{
			if(soal.getId()!=null){
				dao.update(soal);
			}else{
				soal.setKelompok(kelompok.getId());
				soal.setId(new UUIDHexGenerator().generate(null, null).toString().toUpperCase());
				dao.save(soal);
			}
			css = "jwb-view";
//			spawn = true;
		}catch(Exception e){
			e.printStackTrace();
			FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_ERROR,"Error: "+e.getMessage(),""));
			
		}
	}
	public DAOInterface getDao() {
		return dao;
	}

	public void setDao(DAOInterface dao) {
		this.dao = dao;
	}

	public MstUser getUser() {
		return user;
	}

	public void setUser(MstUser user) {
		this.user = user;
	}

	public Kelompok getKelompok() {
		return kelompok;
	}

	public void setKelompok(Kelompok kelompok) {
		this.kelompok = kelompok;
	}

	public List<MstUser> getListAnggota() {
		return listAnggota;
	}

	public void setListAnggota(List<MstUser> listAnggota) {
		this.listAnggota = listAnggota;
	}


	public SoalLks getSoal() {
		return soal;
	}


	public void setSoal(SoalLks soal) {
		this.soal = soal;
	}

	public boolean isSpawn() {
		return spawn;
	}

	public void setSpawn(boolean spawn) {
		this.spawn = spawn;
	}

	public String getCss() {
		return css;
	}

	public void setCss(String css) {
		this.css = css;
	}
	
	
}
